package com.a21200324.upalarm;

import android.content.Context;
import android.util.Log;
import android.view.GestureDetector;
import android.view.MotionEvent;
import android.widget.ViewFlipper;

/**
 * Created by malmeida on 6/22/13.
 */
public class ViewFlipperGesture implements GestureDetector.OnGestureListener {

    private static final String TAG = "ViewFlipperGesture";

    private static final int SWIPE_MIN_DISTANCE = 120;
    private static final int SWIPE_MAX_OFF_PATH = 250;
    private static final int SWIPE_THRESHOLD_VELOCITY = 200;

    private ViewFlipper flipper;
    private Context context;

    public ViewFlipperGesture(Context context, ViewFlipper viewFlipper) {
        this.context = context;
        flipper = viewFlipper;
    }

    @Override
    public boolean onDown(MotionEvent event) {
        return true;
    }

    @Override
    public void onShowPress(MotionEvent event) {

    }

    @Override
    public boolean onSingleTapUp(MotionEvent event) {
        return false;
    }

    @Override
    public boolean onScroll(MotionEvent event, MotionEvent event2, float v, float v2) {
        return true;
    }

    @Override
    public void onLongPress(MotionEvent event) {

    }

    public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {

        Log.d(TAG, "onFling");

        if (Math.abs(e1.getY() - e2.getY()) > SWIPE_MAX_OFF_PATH){
            return false;
        }

        if (e1.getX() - e2.getX() > SWIPE_MIN_DISTANCE && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) {

            flipper.setInAnimation(context, R.anim.in_from_right);
            flipper.setOutAnimation(context, R.anim.out_to_left);
            flipper.showNext();

        } else if (e2.getX() - e1.getX() > SWIPE_MIN_DISTANCE && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) {

            flipper.setInAnimation(context, R.anim.in_from_left);
            flipper.setOutAnimation(context, R.anim.out_to_right);
            flipper.showPrevious();

        }

        return true;
    }
}

